package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.CarBo;
import com.groupseven.happycampus.common.pojo.Car;
import com.groupseven.happycampus.common.pojo.Refund;
import com.groupseven.happycampus.common.result.LayuiPageVo;
import com.groupseven.happycampus.mapper.RefundMapper;
import com.groupseven.happycampus.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("refundService")
public class RefundServiceImpl implements RefundService {
    @Autowired
    private RefundMapper refundMapper;


    @Override
    public LayuiPageVo<Refund> refundPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<Refund> refunds = refundMapper.s(params);

        // mybatis 的 分页数据结构
        PageInfo<Refund> refundPageInfo = new PageInfo<>(refunds);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Refund> refundLayuiPageVo = new LayuiPageVo<>();
        refundLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        refundLayuiPageVo.setMsg("分页列表数据"); // 提示信息


        refundLayuiPageVo.setCount((int) refundPageInfo.getTotal()); // 总记录数


        refundLayuiPageVo.setData(refundPageInfo.getList());  // 分页的列表数据
        return refundLayuiPageVo;
    }

    @Override
    public int deleteRefund(Integer id) {
        return refundMapper.deleteRefund(id);
    }

    @Override
    public int deleteRefundsByIds(String ids) {
        String[] arrId = ids.split(",");
        return refundMapper.deleteRefundsByIds(arrId);
    }

    @Override
    public int addRefund(Map<String, Object> params) {
        int i = refundMapper.addRefund(params);
        return i;
    }

    @Override
    public int updateRefund(Map<String, Object> params) {
        int i = refundMapper.updateRefund(params);
        return i;
    }

    @Override
    public List<CarBo> refundExcelData(Refund refund) {
        // 从数据库查询出来的 是  po  实体类
        List<Refund> refunds = refundMapper.selectRefund(refund);
        // 要把 po 转为 bo
        List<CarBo> refundBos = refunds.stream()
                .map((re) -> {
                    CarBo bo = new CarBo();
                    bo.setType(re.getType());
                    bo.setTime(re.getTime());
                    bo.setPlace(re.getPlace());
                    bo.setDrawback(re.getDrawback());
                    return bo;
                }).collect(Collectors.toList());

        return refundBos;
    }
}
